package introspector;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;
import org.omg.CORBA.StructMember;

public class Demo1 {
	
/**
 * 通过内省api操作bean的name属性
 * @throws Exception 
 */
	@Test
	public void test1() throws Exception{
		Student student= new Student();
		PropertyDescriptor pDescriptor =new PropertyDescriptor("name", Student.class);
		Method method= pDescriptor.getWriteMethod();
		method.invoke(student, "xiayq");
		//System.out.println(student.getName());
		method=pDescriptor.getReadMethod();
		String result=(String) method.invoke(student, null);
		System.out.println(result);
		
	}
	
	//操作bean的所有属性
	
	@Test
	public void test2() throws Exception{
		BeanInfo info=Introspector.getBeanInfo(Student.class);
		PropertyDescriptor pds[]= info.getPropertyDescriptors();
		for(PropertyDescriptor pd:pds){
			System.out.println(pd.getName());
			
		}
	}
	
	
	//由于上述内省方式比较繁琐，sun公司的beanutils工具包比较好用，
	@Test
	public void test3(){
		
		
	}
}
